import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.Authenticator;
import java.util.Properties;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;


import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class SCX {

	final static Properties properties = System.getProperties();

	static {		
//		properties.setProperty("proxySet", "true");
//		properties.setProperty("socksProxyHost", "10.137.1.16");
//		properties.setProperty("socksProxyPort", "1080");		
//		properties.setProperty("socksProxyHost", "10.137.1.16");
//		properties.setProperty("socksProxyPort", "1080");
//
//		properties.setProperty("java.net.socks.username", "osz-domosija");
//		properties.setProperty("java.net.socks.password", "domike08");
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			HttpHost proxy = new HttpHost("10.137.1.16", 1080, "http");
			
			
			final WebClient webClient = new WebClient(BrowserVersion.CHROME_16,
					"10.137.1.16", 3128);			
			
			
			final HtmlPage loginPage = webClient.getPage("http://www.sxc.hu/");
			
			loginPage.getElementsByName("login").get(0).setAttribute("value", "domosi");
			loginPage.getElementsByName("pass").get(0).setAttribute("value", "domike");
			for(DomElement e : loginPage.getElementsByTagName("input")) {
				String src = e.getAttribute("src");
				if(src != null && src.equals("img/btn_submit.gif")) {
					HtmlElement he = (HtmlElement)e;
					System.out.println(he.click().getWebResponse().getContentAsString());
					break;
				}
			}
			
			

			final HtmlPage page = webClient
					.getPage("http://www.sxc.hu/browse.phtml?f=lightboxes&lid=808792");

			for (Object object2 : page
					.getByXPath("//body/div/div/div/div/div/div/div[@class='thumb2']/div/a/img")) {
				HtmlElement listaElement = (HtmlElement) object2;
				String id = listaElement.getAttribute("alt");
				
				final HtmlPage imgPage = webClient
						.getPage("http://www.sxc.hu/browse.phtml?f=download&id=" + id);
				
				HtmlImage imgElement = (HtmlImage) imgPage.getElementsByTagName("img").get(0);
//				String src = imgElement.getAttribute("src");
				String filePath = "/home/osz-domosija/temp/sxc/grocery/" + id + ".jpg";
				System.out.println(filePath);
				try {
					imgElement.saveAs(new File(filePath));
				} catch (Exception e) {					
					e.printStackTrace();
				}
				
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
